package org.txazo.blog.web.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.txazo.blog.util.ContextUtil;

/**
 * Web启动监听器
 * 
 * @author Tu XiaoZhou
 * @since 2013-08-10
 * 
 */
public class ApplicationStartupListener implements ServletContextListener {

	@Override
	public void contextInitialized(ServletContextEvent servletContextEvent) {
		ServletContext context = servletContextEvent.getServletContext();

		String path = context.getRealPath("/").replace('\\', '/');

		if (path.endsWith("/")) {
			path = path.substring(0, path.length() - 1);
		}

		/** Web项目根路径 */
		System.setProperty("web.path", path);

		/** 设置Base Href到Context中，供JSP页面访问 */
		String baseHref = ContextUtil.getBaseHref();
		context.setAttribute("baseHref", baseHref);

		System.setProperty("file.encoding", "UTF-8");
	}

	@Override
	public void contextDestroyed(ServletContextEvent servletContextEvent) {
	}

}
